﻿var express = require("express");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var cookieSession = require("cookie-session");
var multer = require("multer");
var mysql = require("mysql");
var objMulter = multer({ dest: "./www/upload" });
var consolidate = require("consolidate");
var swig = require('swig');

var config = require('./config/config.js');
var db = mysql.createPool({ host: config.host, user: config.user, password: config.password, database: config. database})

var app = express();


// 1、获取数据
app.use(bodyParser.urlencoded({ extended: false }));
app.use(objMulter.any());

// 2、设置cookie、session
app.use(cookieParser());
app.use(cookieSession({
    name: 'sess_id',
    keys: ['aaaa', 'bbbbb', 'ccccc'],
    maxAge: 20 * 60 * 1000	//20分钟
}));

// 3、设置使用的模板引擎
// app.engine('html', consolidate.ejs);
// app.set('views', 'views');
// app.set('view engine', 'html');

app.engine('html', swig.renderFile);
//设置文件存在的目录，第一个参数必须是'views',第二个参数是目录
app.set('views', './views');

//注册所使用的模板引擎，第一个参数必须是 view engine,第二个参数必须和app.engine中第一个参数一致
app.set('view engine', 'html');

swig.setDefaults({ cache: false });

// 设置静态目录
app.use('/www', express.static(__dirname + '/www'));

/*
app.all('*',function(req,res,next){
	res.header("access-control-allow-origin", "*");
	res.header("access-control-allow-headers", "X-Requested-With");
	res.header("access-control-allow-methods", "GET, POST, PUT, DELETE, OPTIONS");
	res.header("Content-Type","application/json;charset=utf-8");
	next();
})*/

// app.use('/adminApi', require('./routers/adminApi'));
app.use('/admin', require('./routers/admin/admin'));
app.use('/login', require('./routers/login'));
app.use('/reg', require('./routers/reg'));


// app.use('/indexApi', require('./routers/indexApi'));
app.use('/', require('./routers/home/index'));



app.listen(8081);

